//
//  WMBaseNavigationViewController.m
//  WaiMai
//
//  Created by Angela on 2017/8/24.
//  Copyright © 2017年 Angela. All rights reserved.
//

#import "WMBaseNavViewController.h"

@interface WMBaseNavViewController ()

@end

@implementation WMBaseNavViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置颜色为黑色
    self.navigationBar.tintColor = [UIColor blackColor];
    self.navigationBar.barStyle = UIBarStyleDefault;
    //当为YES时，使用布局会有问题
    self.navigationBar.translucent = NO;
}
- (void)setCanDragBack:(BOOL)canDragBack
{
    _canDragBack = canDragBack;
    self.fd_fullscreenPopGestureRecognizer.enabled = canDragBack;
}

/**
 * 当第一次使用这个类的时候调用1次
 */
+ (void)initialize
{
    [super initialize];
    // 设置UINavigationBarTheme的主
    [self setupNavigationBarTheme];
}

/**
 * 统一的修改设置UINavigationBarTheme的主题
 */
+ (void)setupNavigationBarTheme
{
    if (![UINavigationBar conformsToProtocol:@protocol(UIAppearanceContainer)]) {
        return;
    }
    UINavigationBar *appearance = [UINavigationBar appearance];
    
    // 设置文字属性
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = UIColorFromRGB(colorA3);
    
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:18];
    [appearance setTitleTextAttributes:textAttrs];
    [appearance setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
}

/**
 * 能拦截所有push进来的子控制器
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.topViewController && [self.topViewController isKindOfClass:[viewController class]]) {
        return;
    }
    
    if (self.viewControllers.count > 0) { // 这时push进来的控制器viewController，不是第一个子控制器（不是根控制器）
        /* 自动显示和隐藏tabbar */
        viewController.hidesBottomBarWhenPushed = YES;
        /* 设置导航栏上面的内容 */
    }
    [super pushViewController:viewController animated:animated];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    if (self.viewControllers.count == 0) return nil;
    UIViewController *viewController = [super popViewControllerAnimated:animated];
    return viewController;
}

- (void)back
{
    [self popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setNeedsStatusBarAppearanceUpdate];
}
- (UIViewController *)childViewControllerForStatusBarStyle
{
    return self.topViewController;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return self.topViewController.preferredStatusBarStyle;
}
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    return [super popToViewController:viewController animated:animated];
}

- (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated
{
    return [super popToRootViewControllerAnimated:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
